<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Model;

use App\Service\QuestionService;
use Hyperf\Database\Model\Events\Saved;

/**
 * @property int $id
 * @property int $category_id 分类ID
 * @property int $user_id 用户ID
 * @property string $title 标题
 * @property string $summary 问题描述
 * @property int $answer_count 回复数
 * @property int $status 0已删除 1正常
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property \App\Model\Category $category
 * @property \App\Model\User $user
 */
class Question extends Model
{
    const STATUS_ONLINE = 1;

    const STATUS_DELETED = 0;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'questions';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'category_id', 'user_id', 'title', 'summary', 'answer_count', 'status', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'category_id' => 'integer', 'user_id' => 'integer', 'answer_count' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function saved(Saved $event)
    {
        di()->get(QuestionService::class)->putToSearch($this->id);
    }
}
